package com.emm.yixun.jobs;

import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.emm.yixun.common.app.base.CommonResDTO;
import com.emm.yixun.common.model.BaseCodeType;
import com.emm.yixun.common.model.BaseCodeValue;
import com.emm.yixun.common.model.BusinessLog;
import com.emm.yixun.common.model.PmoLog;
import com.emm.yixun.common.model.dto.BaseCodeTypeDto;
import com.emm.yixun.common.model.dto.BaseCodeValueDto;
import com.emm.yixun.common.model.dto.BusinessLogDto;
import com.emm.yixun.common.model.dto.PmoLogDto;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.jobs.common.email.MailConfig;
import com.emm.yixun.jobs.common.email.MailUtil;
import com.emm.yixun.system.service.BaseCodeTypeServiceFacade;
import com.emm.yixun.system.service.BaseCodeValueServiceFacade;
import com.emm.yixun.system.service.BusinessLogServiceFacade;
import com.emm.yixun.system.service.NoticeServiceFacade;
import com.emm.yixun.system.service.PmoLogServiceFacade;

/***
 * 逾期未付款推送消息 描述: 通知对象：①经纪人；②销管负责人；3 案场负责人；4 营销负责人
 * 通知内容：你的客户xx（客户名称）已经预期未付款，请尽快处理或催促付款。
 */
@Component
public class ExceptionAlarmJob {
	private static Logger logger = LoggerFactory.getLogger(ExceptionAlarmJob.class);

	@Autowired
	private BaseCodeTypeServiceFacade baseCodeTypeServiceFacade;
	@Autowired
	private BaseCodeValueServiceFacade baseCodeValueServiceFacade;
	@Autowired
	private PmoLogServiceFacade pmoLogServiceFacade;
	@Autowired
	private BusinessLogServiceFacade businessLogServiceFacade;

	@Scheduled(cron = "0 */5 * * * ?")
	public void sendExceptionMail() {
		logger.info("发送业务错误邮件 job start..........");

		MailConfig config = MailUtil.setConfig("mail.properties");
		String sendMailAddresses = "";
		boolean sendFlag = false;
		BaseCodeTypeDto bctDto = new BaseCodeTypeDto();
		bctDto.setCodeType("EM");
		ResponseList<BaseCodeType> bctRes = baseCodeTypeServiceFacade.findByDto(bctDto);
		if (bctRes.isSuccess()) {
			List<BaseCodeType> bctList = bctRes.getEntity();
			if (null != bctList && bctList.size() > 0) {
				BaseCodeType bct = bctList.get(0);
				long bctId = bct.getId();
				BaseCodeValueDto bcvDto = new BaseCodeValueDto();
				bcvDto.setTypeId(bctId);
				ResponseList<BaseCodeValue> bcvRes = baseCodeValueServiceFacade.findByDto(bcvDto);
				if (bctRes.isSuccess()) {
					List<BaseCodeValue> bcvList = bcvRes.getEntity();
					if (null != bcvList && bcvList.size() > 0) {
						for (BaseCodeValue bcv : bcvList) {
							String codeName = bcv.getCodeName();
							String codeValue = bcv.getCodeValue();
							if ("是否发送邮件".equals(codeName)) {
								if ("1".equals(codeValue)) {
									sendFlag = true;
								}
							}
							if ("接收人邮件".equals(codeName) && !StringUtils.isBlank(codeValue)) {
								sendMailAddresses += codeValue + ";";
							}
						}
					}
				}
			}
		}
		
		if(sendFlag && !StringUtils.isBlank(sendMailAddresses)) {
			Calendar calendar = Calendar.getInstance();
			String endTime = DateUtils.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD_HH_MM_SS);
			calendar.add(Calendar.MINUTE, -5);
			String startTime = DateUtils.DateToString(calendar.getTime(), DateStyle.YYYY_MM_DD_HH_MM_SS);
			int pmoLogCount = 0;
			int blCount = 0;
			String errorMsg = "统计时间: " + startTime + " ~ " + endTime +"<br/>";
			PmoLogDto plDto = new PmoLogDto();
			plDto.setStatus("2");
			plDto.setCreateTimeBefore(calendar.getTime());
			ResponseList<PmoLog> pmoLogRes = pmoLogServiceFacade.findByDto(plDto);
			if(pmoLogRes.isSuccess()) {
				List<PmoLog> plList = pmoLogRes.getEntity();
				if(null != plList && plList.size() > 0) {
					pmoLogCount = plList.size();
					errorMsg += "------------------------PMO异常------------------------";
					errorMsg += "PMOLog表错误信息<br/>";
					for(PmoLog pmoLog: plList) {
						errorMsg += pmoLog.getInterfaceName() + ": " + pmoLog.getReturnResult() + "<br/>";
					}
				}
			}
			
			BusinessLogDto blDto = new BusinessLogDto();
			blDto.setCreateTimeBefore(calendar.getTime());
			ResponseList<BusinessLog> blRes = businessLogServiceFacade.findByDto(blDto);
			if(blRes.isSuccess()) {
				List<BusinessLog> blList = blRes.getEntity();
				if(null != blList && blList.size() > 0) {
					blCount = blList.size();
					errorMsg += "------------------------业务异常------------------------";
					errorMsg += "主要业务错误记录:" + blCount + "条";
					for(BusinessLog businessLog: blList){
						if(StringUtils.isNotBlank(businessLog.getException())){

						}
						String businessException = businessLog.getException();
						if(businessException.indexOf("请勿重复提交,请至我的已办查看!") > 0){
							continue;
						}
						errorMsg += "错误Id: " + businessLog.getId() + ", 错误业务: " + businessLog.getBusiness() + "<br />";
					}
				}
			}
			if(pmoLogCount > 0 || blCount > 0) {
				logger.info("..........发送邮件开始..........");
				MailUtil.sendProcess(config, sendMailAddresses, null, null, "异常通知", errorMsg, null);
				logger.info("..........发送邮件结束..........");
			}
		}
		

		logger.info("发送业务错误邮件 job end..........");
	}
	
	public static void main(String[] args) {
		//MailConfig config = MailUtil.setConfig("mail.properties");
		//MailUtil.sendProcess(config, "353895897@qq.com;jianhua.song@qiaofang.info;", null, null, "异常通知c", "有业务出现异常,请检查!", null);


		String aaa = "123123123v请勿重复提交,请至我的已办查看!adfjklajdflkajdslkfa";
		System.out.println(aaa.indexOf("请勿重复提交,请至我的已办查看!"));



	}

}
